#include "ScreenTemplate.h"

ScreenTemplate::ScreenTemplate(QString filePath)
{
    QDomDocument xmlDocument;
    QFile xmlFile(filePath);
    //check to see if the file can be opened and is a text file
    if(!xmlFile.open(QFile::ReadOnly | QFile::Text))return;
    //retrieve information from the file used for versioning
    QFileInfo xmlFileInfo(xmlFile);
    LastSaved = xmlFileInfo.lastModified();
    FileName = xmlFileInfo.fileName();
    //set the content of the xmldocument to the file
    if(!xmlDocument.setContent(&xmlFile))return;
    //get the root of the xmldocument
    QDomElement xmlRoot = xmlDocument.documentElement();
    //set member values
    BackColour = *(new QColor(xmlRoot.attributeNode("BackColour").value()));
    AspectRatio = *(new QString(xmlRoot.attributeNode("AspectRatio").value()));
    //get all the children nodes
    QDomNodeList placeholderNodeList = xmlRoot.childNodes();
    //iterate over each placeholder
    for(int i = 0; i < placeholderNodeList.count(); i++)
    {
        Placeholder * placeholder = new Placeholder();
        //get element version
        QDomElement placeholderElement = placeholderNodeList.at(i).toElement();
        //get all the values from the attributes
        placeholder->X = placeholderElement.attributeNode("X").value().toFloat(NULL);
        placeholder->Y = placeholderElement.attributeNode("Y").value().toFloat(NULL);
        placeholder->Width = placeholderElement.attributeNode("Width").value().toFloat(NULL);
        placeholder->Height = placeholderElement.attributeNode("Height").value().toFloat(NULL);
        //add the placeholder to the vector
        Placeholders.push_back(*placeholder);
    }
    //close the file
    xmlFile.close();
}

ScreenTemplate::~ScreenTemplate()
{
    delete &BackColour;
    delete &AspectRatio;
    //delete all dynamically allocated placeholders in the vector
    for(int i = 0; i < Placeholders.count(); i++)
        delete &Placeholders.at(i);
}
